package hmove;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Image;
import main.*;


public class Move {
    
    private final int PREPAIR = 0;
    private final int FIRE = 1;
    protected int _status;    
    protected int count;
    protected int _damage;
    protected int _weight;
    protected Sprite _sprite;
    protected ThrewObject throwingObject;
    protected Pokemon _pokemonOwner;
    
    public void activate(double thrownPower) {
        
        _sprite.move(_pokemonOwner.getSprite().getRefPixelX() - _sprite.getRefPixelX(),
                _pokemonOwner.getSprite().getRefPixelY() - _sprite.getRefPixelY());
        throwingObject.setThrowParameter(_pokemonOwner.getAngle(), thrownPower);
        _pokemonOwner.Fire();
        count = 0;
        _status = PREPAIR;
        
        
    }

    /*public boolean isActivate() {
    return _sprite.isVisible();
    }
     */
    public void deactivate() {
        throwingObject.deactivate();
        
    }
    
    public boolean run(Sprite map) {
        if (_status == PREPAIR) {
            if (_pokemonOwner.getSprite().getFrame() == _pokemonOwner.getSprite().getFrameSequenceLength() - 1) {
                _status = FIRE;
                throwingObject.activate();
                
            }
            return false;
        } else {
            _pokemonOwner.Stand();
            int x = _sprite.getRefPixelX();
            int y = _sprite.getRefPixelY();
            Point positionPoint = new Point(x, y);
            throwingObject.run(positionPoint);            
            _sprite.nextFrame();
            boolean checkCollides = count > 1;
            count++;
            return (ThrewObject.moveSpriteTo(_sprite, map, positionPoint.getX(), positionPoint.getY(), checkCollides)
                    || positionPoint.getY() > map.getHeight() || positionPoint.getX() < 0 || positionPoint.getX() > map.getWidth());
        }
    }
    
    public Sprite getSprite() {
        return _sprite;
    }
    
    public boolean hit(Pokemon pokemon) {
        if (_sprite.collidesWith(pokemon.getSprite(), true)) {
            pokemon.incHp(-_damage);
            return true;
        }
        return false;
    }
    
    public void paint(Graphics g, int dx, int dy) {
        if (_status == FIRE) {
            _sprite.move(dx, dy);
            _sprite.paint(g);
            _sprite.move(-dx, -dy);
        }
    }
    
    public Sprite getObserveSprite() {
        return null;
    }
}
